//
//  YJBankListView.m
//  YJB
//
//  Created by 中电大维 on 16/9/23.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "YJBankListView.h"
#import "YJUserDefaultTool.h"
#import "YJNetworkTool.h"

#define PICKERVIEWH  216
//工具条高度
#define TOOLBARH 44
@interface YJBankListView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,weak)UIPickerView *pickerView;
@property(nonatomic,strong)NSDictionary *dict;
@property(nonatomic,strong)NSArray *districts;
@property(nonatomic,weak)UIView *toolBar;

//银行列表
@property(nonatomic,strong)NSArray *banks;

@end

@implementation YJBankListView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self loadData];
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        
        //顶部工具条
        UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, IPHONE_H, IPHONE_W, TOOLBARH)];
        toolBar.backgroundColor = [UIColor whiteColor];
        [self addSubview:toolBar];
        _toolBar = toolBar;
        //取消
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 50, toolBar.height);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:cancelBtn];
        
        //确认
        UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        completeBtn.frame = CGRectMake(toolBar.width - 50, 0, 50, toolBar.height);
        [completeBtn setTitle:@"完成" forState:UIControlStateNormal];
        [completeBtn setTitleColor:kUIColorFromRGB(0xff783d) forState:UIControlStateNormal];
        [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:completeBtn];
        
        //标题
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(cancelBtn.width, 0, toolBar.width - 2 *cancelBtn.width, toolBar.height)];
        label.text = @"选择所在银行";
        label.textColor = kUIColorFromRGB(0xff783d);
        label.font = [UIFont systemFontOfSize:18.0f];
        label.textAlignment = NSTextAlignmentCenter;
        [toolBar addSubview:label];
        
        //分割线
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, toolBar.height - 1, toolBar.width, 1)];
        line.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
        [toolBar addSubview:line];
        
        //时间选择器
        UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, IPHONE_H + TOOLBARH, IPHONE_W, PICKERVIEWH)];
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:pickerView];
        _pickerView = pickerView;
        
    }
    return self;
}

-(NSArray *)districts
{
    if (_districts == nil)
    {
        
    }
    return _districts;
}

#pragma mark ----------------------- datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//每一列行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    MyLog(@" _banks.count:%ld",_banks.count);
    return _banks.count;
}

//每一列的View
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
        UILabel *label = [UILabel new];
        label.font = [UIFont systemFontOfSize:16.0f];
      label.text = self.banks[row][@"bank_name"];

    return label;
}

//选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   
        [pickerView reloadComponent:0];
}


#pragma mark ----------------------- 点击触发事件
- (void)show
{
    UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
    [win addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        _toolBar.transform = CGAffineTransformMakeTranslation(0, - PICKERVIEWH - TOOLBARH);
        _pickerView.transform = CGAffineTransformMakeTranslation(0,  - (PICKERVIEWH + TOOLBARH));
    }];
}

- (void)cancelBtnClick
{
    [self dismiss];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
        _toolBar.transform = CGAffineTransformIdentity;
        _pickerView.transform = CGAffineTransformIdentity;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//完成点击事件
- (void)completeBtnClick
{
    
    NSInteger index = [self.pickerView selectedRowInComponent:0];
    //显示bank_name，传出去bank_name,bank_id
    NSString *bankid = _banks[index][@"id"];
    NSString *bankname = _banks[index][@"bank_name"];
   
    NSArray *bankarr = @[bankid,bankname];
     MyLog(@"choice %@",bankarr);
    
    if (self.completeBlock)
        self.completeBlock(bankarr);
    
    [self dismiss];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return (IPHONE_W - 30)/3.0;
}

- (void)loadData
{
    NSMutableDictionary *Params = [NSMutableDictionary dictionary];
    Params[@"action"] = @"get_bank_list";
    Params[@"makegold"] = @{
                            @"user_info":[YJUserDefaultTool readUserName],
                            @"phone_info":@"iOS",
                            @"md5_key":[YJUserDefaultTool readKeyID]
                            };
    [YJNetworkTool postWithUrl:@"http://makegold.a8vsc.com/api/unionpay.php" parameters:Params success:^(id responseObject) {
        NSString *code = responseObject [@"return"][@"code"];
        if (code.intValue == 200)
        {
        
            NSDictionary *userInfoDic = responseObject[@"return"][@"data"];
            self.banks = userInfoDic[@"bank_list"];
            MyLog(@"banks :%@",self.banks);
           [self.pickerView reloadAllComponents];
          
        }
        
    } failure:^(NSError *error)
    {
       
        MyLog(@"网络不佳稍后再试");
    }];
    
}


@end
